﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Alfa.Common;

namespace Alfa.Common.ValidationRules {
	public class StringLengthValidationRule : ValidationRuleBase {
		public StringLengthValidationRule( ) { }

		public long? Min { get; set; }
		public long? Max { get; set; }

		public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
			if (value is string) {
				var textLength = 0;
				if (value != null)
					textLength = ((string)value).Length;

				if (Min > Max)
					throw new Exception("Min length criteria cannot be greater than Max length criteria.");

				if ((Min.HasValue && textLength < Min) || (Max.HasValue && textLength > Max)) {
					return this.ParseError(false, Messages.PleaseEnterATextThatLangthIsInTheRange + (Min.HasValue ? Min.ToString( ) : "") + " - " + (Max.HasValue ? Max.ToString( ) : "") + ".");
				}
			}

			return this.ParseError(true, null);
		}
	}
}
